package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.Orders;
import com.example.demo.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.example.demo.entity.OrderSku;
import com.example.demo.dto.OrderSkuDTO;
import com.example.demo.vo.OrderSkuListVO;
import com.example.demo.vo.OrderSkuDetailVO;
import com.example.demo.service.OrderSkuService;
import com.example.demo.controller.BaseCustomCrudRestController;
import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.util.List;

/**
 * 订单sku 相关Controller
 *
 * @author MyName
 * @version 1.0
 * @date 2024-01-17
 * Copyright © MyCompany
 */
@RestController
@RequestMapping("/orderSku")
@BindPermission(name = "订单sku")
@Slf4j
public class OrderSkuController extends BaseCustomCrudRestController<OrderSku> {

    @Autowired
    private OrderSkuService orderSkuService;

    @Autowired
    private OrdersService ordersService;
    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult<List<OrderSkuListVO>> getViewObjectListMapping(OrderSkuDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, OrderSkuListVO.class);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
        return super.getViewObject(id, OrderSkuDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult<?> createEntityMapping(@Valid @RequestBody OrderSku entity) throws Exception {
        return super.createEntity(entity);
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody OrderSku entity) throws Exception {

        super.updateEntity(id, entity);
        if(entity.getStatus().equals("4")) {
          LambdaQueryWrapper<OrderSku> queryWrapper = new LambdaQueryWrapper<>();
          queryWrapper.eq(OrderSku::getOrdersId, id);
          queryWrapper.ne(OrderSku::getStatus, "4");
          List<OrderSku> orderSkuList = this.getService().getEntityList(queryWrapper);
          if(orderSkuList == null || orderSkuList.size() == 0) {
            UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(Orders::getId, id);
            updateWrapper.set("status", "4");
            ordersService.updateEntity(updateWrapper);
          }

        }
        return JsonResult.OK();
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
        return super.deleteEntity(id);
    }
}
